package cn.itsource.org.controller;

import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.basic.util.AxajResult;
import cn.itsource.basic.util.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//部门controller层
@RestController
@RequestMapping("/department")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IEmployeeService employeeService;
    /**
     *
     * @param department   有ID是修改，没得是保存
     * @return          @RequestBody：前端传入的数据时json必须加此注解
     */
    @PostMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public AxajResult save(@RequestBody Department department) {
        try {
            departmentService.save(department);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }
    /**
     *
     * @param id    通过id删除
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除",notes = "很好用的接口")
    public AxajResult delete(@PathVariable("id")Long id) {
        try {
            departmentService.delete(id);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }

    /**
     *
     * @param id 通过id去查询部门对象
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "单个部门查询",notes = "很好用的接口")
    public Department findById(@PathVariable("id")Long id){
        return departmentService.findById(id);
    }

    /**
     * 查询所有的部门对象
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "查询所有部门",notes = "很好用的接口")
    public PageInfo<Department> findAll(@RequestBody DepartmentQuery departmentQuery){
        //分页查询
        return  departmentService.findAllByQuery(departmentQuery);
    }
    /**
     * 批量删除
     */
    @PatchMapping("/delete")
    @ApiOperation(value = "部门删除",notes = "很好用的接口")
    public AxajResult batchDelete(@RequestBody List<Department> departments) {
        try {
            departmentService.batchDelete(departments);
            return new AxajResult();
        } catch (Exception e) {
            return new AxajResult(false,e.getMessage());
        }
    }

        //加载部门经理
        @PatchMapping("/employees")
        public List<Employee> findEmployees(){
                return employeeService.findAll();
        }
        //查询上级部门
        @PatchMapping("/nextDepartment")
        public List<Department> loadNextDepartment(){
            return departmentService.loadNextDepartment();
        }
}
